package DCAnalyzer;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class DCXMLParser extends DefaultHandler {

	private final static String ns = "dc:";
	private final static boolean DEBUG = false;
	private final static int DATASET_SIZE = /*0*/994;
	List<DCElement> DCElementList;
	private String tempVal;
	// to maintain context
	private DCElement tempElement;
	private boolean prent_flag = false;

	public DCXMLParser() {
		DCElementList = new ArrayList<DCElement>();
	}

	public void runExample() {
		parseDocument();
		printData();
	}

	private void parseDocument() {

		// get a factory
		SAXParserFactory spf = SAXParserFactory.newInstance();
		try {

			// get a new instance of parser
			SAXParser sp = spf.newSAXParser();

			// parse the file and also register this class for call backs
			for(int i = 0; i<=DATASET_SIZE; i++){
				sp.parse(i+".xml", this);
			}

		} catch (SAXException se) {
			se.printStackTrace();
		} catch (ParserConfigurationException pce) {
			pce.printStackTrace();
		} catch (IOException ie) {
			ie.printStackTrace();
		}
	}

	/**
	 * Iterate through the list and print the contents
	 */
	private void printData() {

		System.out.println("No of Elements '" + DCElementList.size() + "'.");

		Iterator<DCElement> it = DCElementList.iterator();
		
		DCElement next = null;

//		if (!DEBUG) {
//			while (it.hasNext()) {
//				next = it.next();
//				//System.out.println(next.toString());
//				System.out.println(next.stats());				
//			}
//		} else {
//			next = it.next();
//			//System.out.println(next.toString());
//			System.out.println(next.stats());
//		}
		
		new DCStats(DCElementList);
	}


	// Event Handlers
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		// reset
		tempVal = "";
		if (qName.equalsIgnoreCase("record")) {
			// create a new instance of element
			tempElement = new DCElement();
			prent_flag = false;
			// tempElement.setType(attributes.getValue("type"));
		}
	}

	public void characters(char[] ch, int start, int length)
			throws SAXException {
		tempVal = new String(ch, start, length);
	}

	public void endElement(String uri, String localName, String qName)
			throws SAXException {

		if (qName.equalsIgnoreCase("record")) {
			// add it to the list
			if(prent_flag){
				DCElementList.add(tempElement);
			}

		} else if (qName.equalsIgnoreCase(ns + "contributor")) {
			tempElement.addContributor(tempVal);
		} else if (qName.equalsIgnoreCase(ns + "coverage")) {
			tempElement.addCoverage(tempVal);
		} else if (qName.equalsIgnoreCase(ns + "creator")) {
			tempElement.addCreator(tempVal);
		} else if (qName.equalsIgnoreCase(ns + "date")) {
			tempElement.addDate(tempVal);
		} else if (qName.equalsIgnoreCase(ns + "format")) {
			tempElement.addFormat(tempVal);
		} else if (qName.equalsIgnoreCase(ns + "identifier")) {
			tempElement.addIdentifier(tempVal);
		} else if (qName.equalsIgnoreCase(ns + "language")) {
			tempElement.addLanguage(tempVal);
		} else if (qName.equalsIgnoreCase(ns + "publisher")) {
			tempElement.addPublisher(tempVal);
		} else if (qName.equalsIgnoreCase(ns + "relation")) {
			tempElement.addRelation(tempVal);
		} else if (qName.equalsIgnoreCase(ns + "rights")) {
			tempElement.addRights(tempVal);
		} else if (qName.equalsIgnoreCase(ns + "source")) {
			tempElement.addSource(tempVal);
		} else if (qName.equalsIgnoreCase(ns + "subject")) {
			tempElement.addSubject(tempVal);
		} else if (qName.equalsIgnoreCase(ns + "title")) {
			tempElement.addTitle(tempVal);
		} else if (qName.equalsIgnoreCase(ns + "type")) {
			tempElement.addType(tempVal);
			if(tempVal.equals("prent")){
				prent_flag = true;
			}
		}

	}

	public static void main(String[] args) {
		DCXMLParser spe = new DCXMLParser();
		spe.runExample();
	}

}
